//所有平台列表
var osArr = new Array();
//所有资料
var detailArr = new Array();
var dateArr = new Array();
var userTotalArr = new Array();
var userTotalDiv = "";

function loadData(data, startDateID, endDateID) {
	var uv, vv, pv, nu, nr, su;
	osArr = new Array();
	osArr = ["iOS Phone", "iOS Pad", "Android Phone", "Android Pad", "WP7 Phone", "FreeWap "];
	detailArr = new Array();
	for(osi=0; osi<6; osi++) {
		detailArr[osi] = new Array();
		detailArr[osi][0] = new Array();
		detailArr[osi][1] = new Array();
		detailArr[osi][2] = new Array();
		detailArr[osi][3] = new Array();
		detailArr[osi][4] = new Array();
		detailArr[osi][5] = new Array();
		detailArr[osi][6] = new Array();
		detailArr[osi][7] = new Array();
	}
	dateArr = new Array();

	if (data.items.length > 0)
	{
		$(data.items).each(function(i, value){
			var myDate=new Date()

			year = value["year"];
			month = value["month"];
			day = value["day"];
			
			if (i == 0)
			{
				$("#"+startDateID).val(year+"/"+month+"/"+day);
			}

			myDate.setFullYear(year,month-1,day);
			dateArr.push(year + "/" + month + "/" + day);

			details = value["data"];
			$(details).each(function(l, dvalue){
				os = dvalue["os"];
				type = dvalue["type"];
				uv = dvalue["uv"]; //每日使用设备数
				pv = dvalue["pv"]; //页面访问量
				vv = dvalue["vv"]; //播放总量
				nu = dvalue["nu"]; //新增设备数
				nr = dvalue["nr"]; //注册用户数
				su = dvalue["su"]; //成功上传量

				if (typeof(dvalue["uv"])=="undefined")
					uv = 0;
				if (typeof(dvalue["vv"])=="undefined")
					vv = 0;
				if (typeof(dvalue["pv"])=="undefined")
					pv = 0;
				if (typeof(dvalue["nu"])=="undefined")
					nu = 0;
				if (typeof(dvalue["nr"])=="undefined")
					nr = 0;
				if (typeof(dvalue["su"])=="undefined")
					su = 0;			


				osi = in_array(os+" "+type, osArr);
				if(osi == -1) {
					osArr.push(os+" "+type);
					osi=osArr.length-1;
					detailArr[osi] = new Array();
					detailArr[osi][0] = new Array();
					detailArr[osi][1] = new Array();
					detailArr[osi][2] = new Array();
					detailArr[osi][3] = new Array();
					detailArr[osi][4] = new Array();
					detailArr[osi][5] = new Array();
					detailArr[osi][6] = new Array();
					detailArr[osi][7] = new Array();
				}

				if(dateArr.length-detailArr[osi][0].length > 1) {
					var size = dateArr.length-detailArr[osi][0].length;
					for(l=1; l<size; l++) {
						detailArr[osi][0].push(0);
						detailArr[osi][1].push(0);
						detailArr[osi][2].push(0);
						detailArr[osi][3].push(0);
						detailArr[osi][4].push(0);
						detailArr[osi][5].push(0);
						detailArr[osi][7].push(0);
					}
				}


				detailArr[osi][0].push(vv);
				detailArr[osi][1].push(uv);
				detailArr[osi][2].push(pv);
				detailArr[osi][3].push(nu);
				detailArr[osi][4].push(nr);
				detailArr[osi][5].push(su);
				if (uv == 0 || vv == 0)
					detailArr[osi][7].push(0);
				else
					detailArr[osi][7].push(parseFloat((vv/uv).toFixed(2)));
				
			});
		});		
		$("#"+endDateID).val(year+"/"+month+"/"+day);
	}
	else {
		alert("选择的时间区间内无数据");	
		$("#"+startDateID).val("");
		$("#"+endDateID).val("");
	}
}


function loadMainOSComp(data) {	
	//读取累计使用设备数
	var tuURL = "http://10.103.13.15:8000/client/mainTotalUser";
	if ($("#MainOSCompEndDate").val() != "")
	{
		tuURL = tuURL + "?endDate=" + $("#MainOSCompEndDate").val();
	}

	userTotalDiv = "MainOSCompPie";
	$.ajax({
		type: "GET",
		url: tuURL,
		cache: true,
		dataType : "jsonp",
		jsonnp: "callback",
		success: onLoadTotalUser
	});
	$(".bitem").removeClass("selbitem");
	$("#mainOSCompTab6").addClass("selbitem");		

	loadData(data, "MainOSCompStartDate", "MainOSCompEndDate");
	if (data.items.length > 0)
	{
		$(".tab-content").show();
		$(".MainOSCompChartTab").hide();
		$("#MainOSCompPieTab").show();
		changeMainOSCompValueType(1);
	}
	else {
		$(".MainOSCompChartTab").hide();
		$(".tab-content").hide();
		
	}

	jQuery('#body-wrapper').hideLoading();
}


//tab切换
function changeMainOSCompValueType(typeValue) {
	var title = "";
	if (typeValue == 0)
		title = "VV";
	else if(typeValue == 1)
		title = "UV";
	else if(typeValue == 2)
		title = "PV";
	else if (typeValue == 3)
		title = "NU";
	else if (typeValue == 6)
		title = "TU";
	else if (typeValue == 7)
	{
		title = "VV/UV";
	}

	if (typeValue == 6)
	{
		genTotalUserPieChart("MainOSCompPie", "主产品线" + title +"百分比图", "截止至" + $("#MainOSCompEndDate").val().replace(/\//g, "-"));
		changeChartType('MainOSComp', 'Pie', typeValue);
		$(".MainOSCompChartTab").hide();
		$("#MainOSCompPieTab").show();
		$("#MainOSCompArea").hide();
	} 
	else if(typeValue == 2 || typeValue == 7) {
		genTable("MainOSCompTable", osArr, dateArr, detailArr, typeValue);
		genLineChart("MainOSCompLine", "主产品线" + title +"趋势图", dateArr, osArr, detailArr, typeValue, $("#MainOSCompStartDate").val().replace(/\//g, "-") + " 至 " + $("#MainOSCompEndDate").val().replace(/\//g, "-"));		
		$(".MainOSCompChartTab").show();
		$("#MainOSCompLine").show();
		$("#MainOSCompArea").hide();
		changeChartType('MainOSComp', 'Line', typeValue)
		$("#MainOSCompPieTab").hide();
		$("#MainOSCompAllTab").hide();
	}
	else {
		genPieChart("MainOSCompPie", "主产品线累计" + title +"百分比图", osArr, detailArr, typeValue, $("#MainOSCompStartDate").val().replace(/\//g, "-") + " 至 " + $("#MainOSCompEndDate").val().replace(/\//g, "-"));
		genAreaChart("MainOSCompArea", "主产品线" + title + "百分比趋势图", dateArr, osArr, detailArr, typeValue, $("#MainOSCompStartDate").val().replace(/\//g, "-") + " 至 " + $("#MainOSCompEndDate").val().replace(/\//g, "-"));
		genTable("MainOSCompTable", osArr, dateArr, detailArr, typeValue);
		genLineChart("MainOSCompLine", "主产品线" + title +"趋势图", dateArr, osArr, detailArr, typeValue, $("#MainOSCompStartDate").val().replace(/\//g, "-") + " 至 " + $("#MainOSCompEndDate").val().replace(/\//g, "-"));
		$(".MainOSCompChartTab").show();
		$("#MainOSCompArea").show();
		changeChartType('MainOSComp', 'Pie', typeValue)

	}
	$(".bitem").removeClass("selbitem");
	$("#mainOSCompTab"+typeValue).addClass("selbitem");		
}

//自选时间区间更新
function updateDateRange(div) {
	var startDate = new Date();
	var endDate = new Date();

	startDate.setFullYear(($("#"+div+"StartDate").val()).substring(0,4), ($("#"+div+"StartDate").val()).substring(5,7)-1,($("#"+div+"StartDate").val()).substring(8,10));
	endDate.setFullYear(($("#"+div+"EndDate").val()).substring(0,4), ($("#"+div+"EndDate").val()).substring(5,7)-1,($("#"+div+"EndDate").val()).substring(8,10));

	days = parseInt((endDate - startDate)/1000/60/60/24)+1;

	if (days > 0)
	{
		getContent(page, "?staDate="+$("#"+div+"StartDate").val()+"&pd="+days);
	}
	else 
		alert("选择区间不对");

	$("."+div+"Date").removeClass("current");

}

//预设时间区域 周、月、季、年、全部
function updatePresetDataRange(linkDiv, days) {
	if (days == "All")
		getContent(page, "");
	else {
		var startDate = new Date()
		startDate.setDate(startDate.getDate()-days);
		getContent(page, "?staDate="+ startDate.getFullYear()+ "/" + (startDate.getMonth()+1)+ "/" + startDate.getDate() +"&pd="+days);
	}
	$("."+linkDiv).removeClass("current");
	$("#"+linkDiv+days).addClass("current");
}


//读取所有用户数
function onLoadTotalUser(data) {
	userTotalArr = new Array();
	var tempArr = new Array();
	$(data.items).each(function(i, value){
		tempArr = new Array();
		tempArr.push(value["os"]+ " " + value["type"]);
		tempArr.push(value["tu"]);
		userTotalArr.push(tempArr);
	});

//	genTotalUserPieChart(userTotalDiv);

}

